﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Pongster
{
    public class ControlManager : List<Control>
    {
        public event EventHandler FocusChanged;
        
        int selectedControl = 0;

        static SpriteFont spriteFont;

        public static SpriteFont SpriteFont
        {
            get { return spriteFont; }
        }

        protected GameRoot GameRef;

        
        //Constructors
        public ControlManager(SpriteFont spriteFont, Game game)
            : base()
        {
            ControlManager.spriteFont = spriteFont;
            GameRef = (GameRoot)game;

            GameRef.SoundManager.LoadSFX("tick", @"Audio/SFX/tick");
            GameRef.SoundManager.LoadSFX("select", @"Audio/SFX/select");

        }

        //public ControlManager(SpriteFont spriteFont, int capacity)
        //    : base(capacity)
        //{
        //    ControlManager.spriteFont = spriteFont;
        //}

        //public ControlManager(SpriteFont spriteFont, IEnumerable<Control> collection)
        //    : base(collection)
        //{
        //    ControlManager.spriteFont = spriteFont;
        //}


        public void Update(GameTime gameTime)
        {
            LinkLabel control;
            LeftRightSelector lrselector;

            if (Count == 0)
                return;

            foreach (Control c in this)
            {
                if (c.Enabled)
                    c.Update(gameTime);

                if (c.HasFocus)
                    c.HandleInput();
            }

            if (InputHandler.KeyPressed(Keys.Up) || InputHandler.KeyReleased(Keys.W))
                PreviousControl();

            if (InputHandler.KeyPressed(Keys.Down) || InputHandler.KeyReleased(Keys.S))
                NextControl();

            foreach (Control c in this)
            {
                if (c is LinkLabel)
                {
                    control = c as LinkLabel;

                    if (!InputHandler.MouseInactive)
                    {
                        if (control.AreaRect.Contains(InputHandler.MousePoint) && control.HasFocus == false)
                        {
                            GameRef.SoundManager.PlayInstantSFX("tick", 0.8f, 0, 0);

                            while (control.HasFocus == false)
                                ChangeControl();
                        }
                    }
                }

                if (c is LeftRightSelector)
                {
                    lrselector = c as LeftRightSelector;

                    if (!InputHandler.MouseInactive)
                    {
                        if (lrselector.AreaRect.Contains(InputHandler.MousePoint) && lrselector.HasFocus == false)
                        {
                            GameRef.SoundManager.PlayInstantSFX("tick", 0.8f, 0, 0);

                            while (lrselector.HasFocus == false)
                                ChangeControl();
                        }
                    }
                }
            }
            
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            
            foreach (Control c in this)
            {
                if (c.Visible)
                    c.Draw(spriteBatch);
            }
        }


        //Methods
        public void NextControl()
        {
            if (Count == 0)
                return;

            int currentControl = selectedControl;

            this[selectedControl].HasFocus = false;

            do
            {
                selectedControl++;

                if (selectedControl == Count)
                    selectedControl = 0;

                if (this[selectedControl].TabStop && this[selectedControl].Enabled)
                {
                    if (FocusChanged != null)
                        FocusChanged(this[selectedControl], null);
                    break;
                }
                 
            } while (currentControl != selectedControl);

            GameRef.SoundManager.PlayInstantSFX("tick", 0.8f, 0, 0);
            this[selectedControl].HasFocus = true;
        }


        public void PreviousControl()
        {
            if (Count == 0)
                return;

            int currentControl = selectedControl;

            this[selectedControl].HasFocus = false;

            do
            {
                selectedControl--;

                if (selectedControl < 0)
                    selectedControl = Count - 1;

                if (this[selectedControl].TabStop && this[selectedControl].Enabled)
                {
                    if (FocusChanged != null)
                        FocusChanged(this[selectedControl], null);
                    break;
                }

            } while (currentControl != selectedControl);

            GameRef.SoundManager.PlayInstantSFX("tick", 0.8f, 0, 0);
            this[selectedControl].HasFocus = true;
        }

        public void ChangeControl()
        {
            if (Count == 0)
                return;

            int currentControl = selectedControl;

            this[selectedControl].HasFocus = false;

            do
            {
                selectedControl++;

                if (selectedControl == Count)
                    selectedControl = 0;

                if (this[selectedControl].TabStop && this[selectedControl].Enabled)
                {
                    if (FocusChanged != null)
                        FocusChanged(this[selectedControl], null);
                    break;
                }

            } while (currentControl != selectedControl);

            this[selectedControl].HasFocus = true;
        }


    }
}
